//
//  Cell.m
//  下拉菜单
//
//  Created by NewAge on 16/8/19.
//  Copyright © 2016年 new. All rights reserved.
//

#import "Cell.h"
#import "TreeViewNode.h"

@interface Cell ()

@property (nonatomic, strong) UILabel * label;

@end

@implementation Cell

- (UIButton *)button
{
    if (_button == nil) {
        _button = [[UIButton alloc] init];
        [_button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
//        [_button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
    
}

- (UILabel *)label
{
    if (_label == nil) {
        _label = [[UILabel alloc] init];
        _label.textColor = [UIColor purpleColor];
    }
    return _label;
}

- (void)drawRect:(CGRect)rect
{
    //缩进
    CGRect cellFrame = self.label.frame;
    CGRect buttonFrame = self.button.frame;
    NSUInteger indentation = self.node.nodeLevel * 25;
    cellFrame.origin.x = buttonFrame.size.width + indentation + 5;
    buttonFrame.origin.x = 2 + indentation;
    self.label.frame = cellFrame;
    self.button.frame = buttonFrame;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self addUI];
    }
    return self;
}

- (void) addUI
{
    
    [self addSubview:self.button];
    [self addSubview:self.label];
    
    
}

- (void)setNode:(TreeViewNode *)node
{
    _button.userInteractionEnabled = NO;//button不能点击
    _node = node;
    self.button.frame = CGRectMake(10, 10, 60, 30);
    self.label.frame = CGRectMake(CGRectGetMaxX(self.button.frame) + 5, 10,self.frame.size.width - 80, 30);
    
    self.label.text = node.nodeName;
    
}

- (void)buttonClick:(id)sender
{
    self.node.isExpanded = !self.node.isExpanded;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"TreeNodeClick" object:nil];
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
